<template>
  <div>
    <button @click="get_websocket_msg">
      <i class="el-icon-caret-top"></i>
      连接
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  mounted: function () {
    this.get_websocket_msg()
  },
  methods: {
    get_websocket_msg() {
      let ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
      let ws = new WebSocket("ws://127.0.0.1:8000/websocket/connect/");
      ws.onopen = function () {
        console.log("连接成功");
        ws.send("connect");    
      };
      ws.onmessage = function (evt) {
        //获取服务器发来的消息
        var received_msg = evt.data;
        console.log(received_msg)
      }
    },
  },
};
</script>

<style>
</style>